import React from 'react'
import Component, { mapStateToProps, mapDispatchToProps } from './Component'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import TableTags from './TableTags'
import CaseNote from './CaseNote/Index'

const outsideList = ['港', '澳', '台', '外国']

class CaseNoRender extends Component {
  // 前往系列案列表
  toSeriesCase (data) {
    this.props.addWindow({ id: `seriesCaseDetail-${data.id}`, title: `系列案-${data.serieno}`, componentName: 'SeriesCaseDetail', forceRefresh: false, meta: { id: data.id, editable: false } })
  }

  // 获取标签
  getTags (d) {
    const showTags = []
    const noticeMap = {}
    const partyAreas = [...(d.applicants || []), ...(d.respondents || [])].map(li => li.area)
    const hasOut = partyAreas.some(li => outsideList.indexOf(li) !== -1)
    if (d.delay_day) {
      showTags.push('缓')
    }
    if (d.serieno) {
      showTags.push('系')
      noticeMap['系'] = {
        title: `系列号：${d.serieno}`,
        clickHandle: () => this.toSeriesCase(d.serienoInfo)
      }
    }
    if (hasOut || d.foreign_related_type) {
      showTags.push('外')
      noticeMap['外'] = {
        title: d.foreign_related_type || '主体涉外'
      }
    }
    if (d.casetype === '金融案件') {
      showTags.push('金')
      noticeMap['金'] = {
        title: d.casesubtype
      }
    }
    if (d.counter_caseno_desc) {
      showTags.push('反')
      noticeMap['反'] = {
        title: `反请求案号：${d.counter_caseno_desc}`
      }
    }
    if (this.props.note && d.note > 0) {
      showTags.push('笺')
      noticeMap['笺'] = {
        Component: CaseNote
      }
    }
    return {
      showTags,
      noticeMap
    }
  }

  render () {
    const d = this.props.data
    const caseno = d.caseno_desc || d.case_no || d.acceptno_desc || d.acceptno
    const { showTags, noticeMap } = this.getTags(d)
    return (
      <span>
        <TableTags tags={['缓', '系', '金', '外', '反', '变', '海丝', '国', '知', '海', '笺']} showTags={showTags} noticeMap={noticeMap} caseid={this.props.data.caseid || this.props.data.id} caseno={caseno} refreshFn={() => { this.props.refreshFn() }} />
        <span>{caseno}</span>
      </span>
    )
  }
}

TableTags.propTypes = {
  data: PropTypes.object
}

TableTags.defaultProps = {
  data: {}
}

const mapState = mapStateToProps(function (state) {
  return {

  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(CaseNoRender)
